package com.wangsh.usersCenter.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsh.usersCenter.common.pojo.ApiResponse;
import com.wangsh.usersCenter.common.pojo.ApiResponseEnum;
import com.wangsh.usersCenter.common.pojo.JSONEnum;
import com.wangsh.usersCenter.common.service.BaseServiceImpl;
import com.wangsh.usersCenter.common.util.ConstatFinalUtil;
import com.wangsh.usersCenter.common.util.HttpUtil;
import com.wangsh.usersCenter.common.util.PageInfoUtil;
import com.wangsh.usersCenter.system.dao.IACateDAO;
import com.wangsh.usersCenter.system.dao.IARegionDAO;
import com.wangsh.usersCenter.system.dao.IASysIpDAO;
import com.wangsh.usersCenter.system.dao.IASysLogDAO;
import com.wangsh.usersCenter.system.dao.IASysProDAO;
import com.wangsh.usersCenter.system.pojo.ACate;
import com.wangsh.usersCenter.system.pojo.ACateEnum;
import com.wangsh.usersCenter.system.pojo.ARegion;
import com.wangsh.usersCenter.system.pojo.ARegionEnum;
import com.wangsh.usersCenter.system.pojo.ASysIp;
import com.wangsh.usersCenter.system.pojo.ASysIpEnum;
import com.wangsh.usersCenter.system.pojo.ASysLog;
import com.wangsh.usersCenter.system.pojo.ASysPro;
import com.wangsh.usersCenter.system.pojo.ASysProEnum;
import com.wangsh.usersCenter.system.service.ISystemDbService;
import com.wangsh.usersCenter.users.pojo.AAdmins;
import com.wangsh.usersCenter.users.service.IUsersDbService;

/**
 * 系统模块的实现类
 * 
 * @author wangshh
 * 
 */
@Service("systemDbService")
public class SystemDbServiceImpl extends BaseServiceImpl  implements ISystemDbService  
{
	@Resource
	private IARegionDAO regionDAO;
	@Resource
	private IASysLogDAO logDAO;
	@Resource
	private IACateDAO cateDAO;
	@Resource
	private IASysProDAO sysProDao;
	@Resource
	private IASysIpDAO sysIpDAO;
	@Resource
	private HttpUtil httpUtil;
	@Resource
	private IUsersDbService usersDbService;
	@Resource
	private ISystemDbService systemDbService;
	
	/**
	 * 初始化操作
	 */
	@PostConstruct
	public ApiResponse<Object> reloadSystem()
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 项目名字 */
		String projectName = ConstatFinalUtil.CONFIG_JSON.getString("project.name");
		/* 查询所有启用的共享变量,系统配置 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("status", ASysProEnum.STATUS_ENABLE.getStatus());
		ApiResponse<ASysPro> response = this.findCondListSysProService(null, condMap);
		List<ASysPro> sysProList = response.getDataListJava();
		/* 循环放到Map中 */
		for (Iterator iterator = sysProList.iterator(); iterator.hasNext();)
		{
			ASysPro sysPro = (ASysPro) iterator.next();
			this.redisUtil.put(projectName + "_" + sysPro.getEngName(), sysPro.toJSON().toJSONString());
		}
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveOneRegionService(ARegion region)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据pinyin查询,用来判断去重 */
		condMap.clear();
		condMap.put("areacode", region.getAreacode());
		ApiResponse<ARegion> resApiResponse = this.findOneRegionService(condMap);
		/* 查询原来数据 */
		ARegion regionRes = resApiResponse.getDataOneJava();
		if(regionRes != null)
		{
			apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
			/* 说明数据已经存在 */
			return apiResponse ;
		}
		
		/* 处理树形结构 */
		condMap.clear();
		condMap.put("id", region.getParentId());
		ApiResponse<ARegion> apiResponseRes = this.findOneRegionService(condMap);
		regionRes = apiResponseRes.getDataOneJava();
		if (regionRes != null)
		{
			/* 更新父节点的叶子节点 */
			if (regionRes.getLeafStatus() == ARegionEnum.LEAFSTATUS_FALSE.getStatus())
			{
				regionRes.setLeafStatus(ARegionEnum.LEAFSTATUS_TRUE.getStatus());
				regionRes.setUpdateTime(new Date());
				this.updateOneRegionService(regionRes);
			}
		}

		int res = this.regionDAO.save(region);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), region.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), region);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneRegionService(ARegion region)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 处理树形结构 */
		condMap.clear();
		condMap.put("id", region.getParentId());
		ApiResponse<ARegion> apiResponseRes = this.findOneRegionService(condMap);
		ARegion regionRes = apiResponseRes.getDataOneJava();
		if (regionRes != null)
		{
			/* 更新父节点的叶子节点 */
			if (regionRes.getLeafStatus() == ARegionEnum.LEAFSTATUS_FALSE.getStatus())
			{
				regionRes.setLeafStatus(ARegionEnum.LEAFSTATUS_TRUE.getStatus());
				regionRes.setUpdateTime(new Date());
				this.updateOneRegionService(regionRes);
			}
		}
		
		int res = this.regionDAO.update(region);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), region.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), region);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneRegionService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.regionDAO.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ARegion> findCondListRegionService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<ARegion> apiResponse = new ApiResponse<ARegion>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ARegion> regionList = Collections.EMPTY_LIST;

		Map<String, List<ARegion>> dataList = new HashMap<String, List<ARegion>>();
		/* 对关键字进行模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/* 设置分页相关信息  pageInfoUtil在创建的时候有默认值,永远不可能为null*/
		if (pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			// 这一步之后regionList的toString会带上一个page的信息 
			regionList = this.regionDAO.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		} else 
		{
			regionList = this.regionDAO.findList(condMap);
		}

		if ("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询向上的所有节点的名字 */
			for (Iterator iterator = regionList.iterator(); iterator.hasNext();)
			{
				ARegion region = (ARegion) iterator.next();
				this.findTreeRegionService(region);
			}
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), regionList);

		apiResponse.setDataListJava(regionList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ARegion> findOneRegionService(Map<String, Object> condMap)
	{
		ApiResponse<ARegion> apiResponse = new ApiResponse<ARegion>();
		Map<String, ARegion> data = new HashMap<String, ARegion>();
		ARegion region = this.regionDAO.findOne(condMap);

		if (region != null)
		{
			/* 查询当前节点的子节点 */
			condMap.clear();
			condMap.put("parentId", region.getId() + "");
			condMap.put("status", ARegionEnum.STATUS_ENABLE.getStatus() + "");
			apiResponse = this.findCondListRegionService(null, condMap);
			List<ARegion> childRegionList = apiResponse.getDataListJava();
			/* 设置子节点 */
			region.setChildrenList(childRegionList);
			if (region.getId() != 0)
			{
				/* 查询所有的上级节点 */
				apiResponse = this.findTreeRegionService(region);
			}
		}

		data.put(ApiResponseEnum.NAME_ONE.getName(), region);

		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(region);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ARegion> findTreeRegionService(ARegion region)
	{
		ApiResponse<ARegion> apiResponse = new ApiResponse<ARegion>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ARegion> regionList = new ArrayList<ARegion>();
		/* 存储数据 */
		Map<String, ARegion> dataOne = new HashMap<String, ARegion>();
		if (region != null)
		{
			this.findTreeRegionServiceUtil(regionList, region);
			Collections.reverse(regionList);

			// 获得字符串表示
			StringBuffer ressb = new StringBuffer();
			for (Iterator iterator = regionList.iterator(); iterator.hasNext();)
			{
				ARegion regTemp = (ARegion) iterator.next();
				ressb.append(regTemp.getName() + ConstatFinalUtil.SPLIT_STR);
			}

			if (!"".equalsIgnoreCase(ressb.toString()))
			{
				ressb.delete(ressb.lastIndexOf(ConstatFinalUtil.SPLIT_STR), ressb.length());
			}
			region.setTreeName(ressb.toString());
			/* 所有的二级结构 */
			region.setTreeRegion(regionList);

			/* 将结果放到JSON中   不停的循环 dataOne的值不停的换,这一步 没啥用*/
			dataOne.put(ApiResponseEnum.NAME_ONE.getName(), region);
			apiResponse.setDataOne(dataOne);
		}
		return apiResponse;
	}

	/**
	 * 计算树形结构的工具方法,一级节点-->二级节点-->三级节点
	 * 
	 * @param ressb
	 * @param regionid
	 */
	private void findTreeRegionServiceUtil(List<ARegion> regList, ARegion region)
	{
		if (region == null)
		{
			return;
		}
		regList.add(region);
		if (region.getParentId() != 0)
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", region.getParentId() + "");
			ApiResponse<ARegion> apiResponse = this.findOneRegionService(condMap);
			ARegion regionRes = apiResponse.getDataOneJava();
			// ARegion regionRes = ConstatFinalUtil.regionMap.get(region.getParentid());
			findTreeRegionServiceUtil(regList, regionRes);
		}
	}

	/*获取地区所有的子节点一直到没有后代*/
	public void getRegionChildrenList(List<ARegion> regList,ARegion region)
	{	
		Map<String, Object> condMap = new HashMap<String, Object>();
		if(region == null)
		{
			return ; 
		}
		List<ARegion> childrenList = region.getChildrenList();
		regList.add(region);
		if (childrenList.isEmpty())
		{
			return;
		}
		for (ARegion aRegion : childrenList)
		{
			condMap.put("parentId",aRegion.getId());
			condMap.put("status", ARegionEnum.STATUS_ENABLE.getStatus() + "");
			ApiResponse<ARegion> findCondListRegionService = this.findCondListRegionService(null, condMap);
			if (!findCondListRegionService.getDataListJava().isEmpty())
			{
				List<ARegion> dataListJava = findCondListRegionService.getDataListJava();
				for (ARegion aRegion2 : dataListJava)
				{
					condMap.clear();
					condMap.put("id", aRegion2.getId());
					ApiResponse<ARegion> findOneRegionService = this.findOneRegionService(condMap);
					this.getRegionChildrenList(regList, findOneRegionService.getDataOneJava());
				}
			} else 
			{ 
				regList.add(aRegion);
			}
				 
		}
	}
	
	@Override
	public ApiResponse<Object> saveOneLogService(ASysLog log)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.logDAO.save(log);
		if (res > 0)
		{
			/*存储Sysip信息
			 * */
			ASysIp sysIp = new ASysIp();
			sysIp.setName(log.getIp());
			sysIp.setIp(log.getIp());
			sysIp.setStatus(ASysIpEnum.STATUS_DISABLE.getStatus());
			sysIp.setCreateTime(new Date());
			sysIp.setUpdateTime(new Date());
			sysIp.setPubTime(new Date());
			ApiResponse<Object> sysIpApiResponse = this.systemDbService.saveOneSysIpService(sysIp);
			ConstatFinalUtil.SYS_LOGGER.info("存储sysip,结果:{}",sysIpApiResponse.toJSON().toJSONString());
			
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), log.getId());
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneLogService(ASysLog log)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("email", log.getName());
		ApiResponse<ASysLog> apiRes = this.findOneLogService(condMap);
		/* 获取对象 */
		ASysLog logRes = apiRes.getDataOneJava();
		if (logRes != null && logRes.getId() != logRes.getId())
		{
			/* 用户名已经存在,请换一个 */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
			return apiResponse;
		}

		int res = this.logDAO.update(log);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), log.getId());
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneLogService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.logDAO.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ASysLog> findCondListLogService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<ASysLog> apiResponse = new ApiResponse<ASysLog>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ASysLog> logList = Collections.EMPTY_LIST;

		Map<String, List<ASysLog>> dataList = new HashMap<String, List<ASysLog>>();
		/* 对关键字进行模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/* 设置分页相关信息 */
		if (pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			logList = this.logDAO.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		} else
		{
			logList = this.logDAO.findList(condMap);
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), logList);

		apiResponse.setDataListJava(logList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ASysLog> findOneLogService(Map<String, Object> condMap)
	{
		ApiResponse<ASysLog> apiResponse = new ApiResponse<ASysLog>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		Map<String, ASysLog> data = new HashMap<String, ASysLog>();
		ASysLog log = this.logDAO.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), log);

		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(log);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> saveOneCateService(ACate cate)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.cateDAO.save(cate);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), cate.getId());
			apiResponse.setDataOne(dataMap);
			
			/* 添加当前节点,如果父节点要是能够查询到,将父节点直接变成非叶子节点 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", cate.getParentId());
			ApiResponse<ACate> parentCateResponse = this.findOneCateService(condMap);
			ACate parentCate = parentCateResponse.getDataOneJava();
			if (parentCate.getId() != 0)
			{
				parentCate.setLeafStatus(ACateEnum.LEAFSTATUS_FALSE.getStatus());
				this.updateOneCateService(parentCate);
			}
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneCateService(ACate cate)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("name", cate.getName());
		ApiResponse<ACate> apiRes = this.findOneCateService(condMap);
		/* 获取对象 */
		ACate cateRes = apiRes.getDataOneJava();
		if (cateRes != null && cateRes.getId() != cate.getId())
		{
			/* 用户名已经存在,请换一个 */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
			return apiResponse;
		}

		int res = this.cateDAO.update(cate);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), cate.getId());
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneCateService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.cateDAO.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ACate> findCondListCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<ACate> apiResponse = new ApiResponse<ACate>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ACate> cateList = Collections.EMPTY_LIST;

		Map<String, List<ACate>> dataList = new HashMap<String, List<ACate>>();
		/* 对关键字进行模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/* 设置分页相关信息 */
		if (pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			cateList = this.cateDAO.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		} else
		{
			cateList = this.cateDAO.findList(condMap);
		}
		if ("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询向上的所有节点的名字 */
			for (Iterator iterator = cateList.iterator(); iterator.hasNext();)
			{
				ACate cate = (ACate) iterator.next();
				this.findTreeCateService(cate);
			}
		}
		condMap.clear();
		for (ACate aCate : cateList)
		{
			condMap.put("id", aCate.getCreateId());
			ApiResponse<AAdmins> findOneAdminsService = this.usersDbService.findOneAdminsService(condMap);
			aCate.setCreateAdmins(findOneAdminsService.getDataOneJava());
			condMap.put("id", aCate.getUpdateId());
			ApiResponse<AAdmins> findOneAdminsService2 = this.usersDbService.findOneAdminsService(condMap);
			aCate.setUpdateAdmins(findOneAdminsService2.getDataOneJava());
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), cateList);

		apiResponse.setDataListJava(cateList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ACate> findOneCateService(Map<String, Object> condMap)
	{
		ApiResponse<ACate> apiResponse = new ApiResponse<ACate>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		Map<String, ACate> data = new HashMap<String, ACate>();
		String cateType = condMap.get("cateType") + ""; 
		ACate cate = this.cateDAO.findOne(condMap);
//		if (cate == null)
//		{
//			cate = new ACate();
//			cate.setId(Integer.valueOf(condMap.get("id") + ""));
//		}
		if(cate != null)
		{
			condMap.clear();
			condMap.put("parentId", cate.getId() + "");
			condMap.put("cateType", cateType );
			
			apiResponse = this.findCondListCateService(null, condMap);
			List<ACate> list = apiResponse.getDataList().get("list");
			cate.setChildrenList(list);
			if (cate.getId() != 0)
			{
				/* 查询所有的上级节点 */
				apiResponse = this.findTreeCateService(cate);
				condMap.put("id", cate.getCreateId());
				ApiResponse<AAdmins> findOneAdminsService = this.usersDbService.findOneAdminsService(condMap);
				cate.setCreateAdmins(findOneAdminsService.getDataOneJava());
				condMap.put("id", cate.getUpdateId());
				ApiResponse<AAdmins> findOneAdminsService2 = this.usersDbService.findOneAdminsService(condMap);
				cate.setUpdateAdmins(findOneAdminsService2.getDataOneJava());
				data.put(ApiResponseEnum.NAME_ONE.getName(), cate);
			}
		}

		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(cate);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<ACate> findTreeCateService(ACate cate)
	{
		ApiResponse<ACate> apiResponse = new ApiResponse<ACate>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ACate> cateList = new ArrayList<ACate>();
		/* 存储数据 */
		Map<String, ACate> dataOne = new HashMap<String, ACate>();
		if (cate != null)
		{
			this.findTreeCateServiceUtil(cateList, cate);
			Collections.reverse(cateList);

			// 获得字符串表示
			StringBuffer ressb = new StringBuffer();
			for (Iterator iterator = cateList.iterator(); iterator.hasNext();)
			{
				ACate regTemp = (ACate) iterator.next();
				ressb.append(regTemp.getName() + ConstatFinalUtil.SPLIT_STR);
			}

			if (!"".equalsIgnoreCase(ressb.toString()))
			{
				ressb.delete(ressb.lastIndexOf(ConstatFinalUtil.SPLIT_STR), ressb.length());
			}
			cate.setTreeName(ressb.toString());
			/* 所有的二级结构 */
			cate.setTreeCate(cateList);
			
			/* 将结果放到JSON中   不停的循环 dataOne的值不停的换,这一步 没啥用*/
			dataOne.put(ApiResponseEnum.NAME_ONE.getName(), cate);
			apiResponse.setDataOne(dataOne);
		}
		return apiResponse;
	}

	/**
	 * 计算树形结构的工具方法,一级节点-->二级节点-->三级节点
	 * 
	 * @param ressb
	 * @param cateid
	 */
	private void findTreeCateServiceUtil(List<ACate> regList, ACate cate)
	{
		if (cate == null)
		{
			return;
		}
		regList.add(cate);
		if (cate.getParentId() != 0)
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", cate.getParentId() + "");
			ApiResponse<ACate> apiResponse = this.findOneCateService(condMap);
			ACate cateRes = apiResponse.getDataOneJava();
			cate.setParentCate(cateRes);
			// ACate cateRes = ConstatFinalUtil.cateMap.get(cate.getParentid());
			findTreeCateServiceUtil(regList, cateRes);
		}
	}

	@Override
	public ApiResponse<Object> saveOneSysProService(ASysPro sysPro)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.sysProDao.save(sysPro);
		if (res > 0)
		{
			/* 项目名字 */
			String projectName = ConstatFinalUtil.CONFIG_JSON.getString("project.name");
			
			/* 存储到redis中 */
			this.redisUtil.put(projectName + "_" + sysPro.getEngName() , sysPro.toJSON().toJSONString());
			
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), sysPro.getId());
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneSysProService(ASysPro sysPro)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		int res = this.sysProDao.update(sysPro);
		if (res > 0)
		{
			/* 项目名字 */
			String projectName = ConstatFinalUtil.CONFIG_JSON.getString("project.name");
			/* 更新json */
			this.redisUtil.put(projectName + "_" + sysPro.getEngName() , sysPro.toJSON().toJSONString());
			
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), sysPro.getId());
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;

	}

	@Override
	public ApiResponse<Object> deleteOneSysProService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.sysProDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ASysPro> findCondListSysProService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<ASysPro> apiResponse = new ApiResponse<ASysPro>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ASysPro> sysProList = Collections.EMPTY_LIST;

		Map<String, List<ASysPro>> dataList = new HashMap<String, List<ASysPro>>();
		/* 对关键字进行模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/* 设置分页相关信息 */
		if (pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			sysProList = this.sysProDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		} else
		{
			sysProList = this.sysProDao.findList(condMap);
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), sysProList);

		apiResponse.setDataListJava(sysProList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ASysPro> findOneSysProService(Map<String, Object> condMap)
	{
		ApiResponse<ASysPro> apiResponse = new ApiResponse<ASysPro>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		Map<String, ASysPro> data = new HashMap<String, ASysPro>();
		
		ASysPro sysPro = new ASysPro() ; 
		if(condMap.get("cacheName") != null && !"".equalsIgnoreCase(condMap.get("cacheName") + ""))
		{
			/* 项目名字 */
			String projectName = ConstatFinalUtil.CONFIG_JSON.getString("project.name");
			
			/* 说明从缓存中取数据 */
			/* 先从缓存中取数据 */
			String cacheName = condMap.get("cacheName") + "";
			/* 获取缓存 */
			String resultStr = this.redisUtil.get(projectName + "_" + cacheName) + "";
			/* 把字符串解析为java对象 */
			JSONObject dataJSON = (JSONObject) JSON.parse(resultStr);
			/* 从json字符串中解析出一个对象 */
			sysPro.parseJSON(dataJSON);
		}else
		{
			sysPro = this.sysProDao.findOne(condMap);
		}
		data.put(ApiResponseEnum.NAME_ONE.getName(), sysPro);
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(sysPro);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	// ip
	@Override
	public ApiResponse<Object> saveOneSysIpService(ASysIp aSysIp)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		if (aSysIp.getIp() == null || "".equalsIgnoreCase(aSysIp.getIp()))
		{
			/* ip不能为空 */
			/* 记录已经存在 */
			apiResponse.setCode(ApiResponseEnum.INFO_ERROR_FORMAT.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
			return apiResponse;
		}
		/* 根据ip查询,ip永远不能为空 */
		condMap.clear();
		condMap.put("ip", aSysIp.getIp());
		ApiResponse<ASysIp> apiResponseRes = this.findOneSysIpService(condMap);
		ASysIp sysIpRes = apiResponseRes.getDataOneJava();
		if (sysIpRes != null)
		{
			/* 记录已经存在 */
			apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
			return apiResponse;
		}

		aSysIp.setCreateTime(new Date());
		aSysIp.setUpdateTime(new Date());

		int res = this.sysIpDAO.save(aSysIp);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), aSysIp.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), aSysIp);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneSysIpService(ASysIp sysIp)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		sysIp.setUpdateTime(new Date());
		int res = this.sysIpDAO.update(sysIp);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), sysIp.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), sysIp);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneSysIpService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.sysIpDAO.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ASysIp> findCondListSysIpService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<ASysIp> apiResponse = new ApiResponse<ASysIp>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ASysIp> sysIpList = Collections.EMPTY_LIST;

		Map<String, List<ASysIp>> dataList = new HashMap<String, List<ASysIp>>();
		/* 对关键字进行模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/* 设置分页相关信息 */
		if (pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			sysIpList = this.sysIpDAO.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		} else
		{
			sysIpList = this.sysIpDAO.findList(condMap);
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), sysIpList);

		apiResponse.setDataListJava(sysIpList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ASysIp> findOneSysIpService(Map<String, Object> condMap)
	{
		ApiResponse<ASysIp> apiResponse = new ApiResponse<ASysIp>();
		Map<String, ASysIp> data = new HashMap<String, ASysIp>();
		ASysIp sysIp = this.sysIpDAO.findOne(condMap);
		
		if(sysIp != null)
		{
			/* 查询一下树形结构的地区 */
			Map<String, Object> extendMap = new HashMap<String, Object>();
			extendMap.put("id", sysIp.getRegionId());
			ApiResponse<ARegion> apiResponseReg = this.findOneRegionService(extendMap);
			ARegion region = apiResponseReg.getDataOneJava() ; 
			sysIp.setRegion(region);
		}
		
		data.put(ApiResponseEnum.NAME_ONE.getName(), sysIp);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(sysIp);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}
}
